package com.itheima.project.face.impl;

import com.itheima.project.pojo.ContentChannel;
import com.itheima.project.service.IContentChannelService;
import com.itheima.project.vo.content.ContentChannelVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.project.utils.BeanConv;
import com.itheima.project.face.ContentChannelFace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import com.itheima.project.utils.ExceptionsUtil;
import com.itheima.project.exception.ProjectException;
import com.itheima.project.constant.content.ContentChannelCacheConstant;
import com.itheima.project.core.enums.content.ContentChannelEnum;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
/**
 * @Description：交易渠道表Face服务实现类
 */
@Slf4j
@Component
public class ContentChannelFaceImpl implements ContentChannelFace {

    @Autowired
    IContentChannelService contentChannelService;


    @Override
    @Cacheable(value = ContentChannelCacheConstant.PAGE,key ="#pageNum+'-'+#pageSize+'-'+#contentChannelVo.hashCode()")
    public Page<ContentChannelVo> findContentChannelVoPage(ContentChannelVo contentChannelVo, int pageNum, int pageSize) {
        try {
            Page<ContentChannel> page = contentChannelService.findContentChannelPage(contentChannelVo, pageNum, pageSize);
            Page<ContentChannelVo> pageVo =BeanConv.toPage(page,ContentChannelVo.class);
            return pageVo;
        }catch (Exception e){
            log.error("交易渠道表列表异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(ContentChannelEnum.PAGE_FAIL);
        }
    }

    @Transactional
    @Caching(evict = {@CacheEvict(value = ContentChannelCacheConstant.PAGE,allEntries = true),
        @CacheEvict(value = ContentChannelCacheConstant.LIST,allEntries = true)},
    put={@CachePut(value =ContentChannelCacheConstant.BASIC,key = "#result.id")})
    @Override
    public ContentChannelVo createContentChannelVo(ContentChannelVo contentChannelVo) {
        try {
            ContentChannelVo contentChannelVoResult = BeanConv.toBean(contentChannelService.createContentChannel(contentChannelVo), ContentChannelVo.class);
            return contentChannelVoResult;
        } catch (Exception e) {
            log.error("保存交易渠道表异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(ContentChannelEnum.SAVE_FAIL);
        }
    }

    @Transactional
    @Caching(evict = {@CacheEvict(value = ContentChannelCacheConstant.PAGE,allEntries = true),
        @CacheEvict(value = ContentChannelCacheConstant.LIST,allEntries = true),
        @CacheEvict(value =ContentChannelCacheConstant.BASIC,key = "#contentChannelVo.id")})
    @Override
    public Boolean updateContentChannelVo(ContentChannelVo contentChannelVo) {
        try {
            Boolean flag = contentChannelService.updateContentChannel(contentChannelVo);
            return flag;
        } catch (Exception e) {
            log.error("修改交易渠道表异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(ContentChannelEnum.UPDATE_FAIL);
        }
    }

    @Transactional
    @Caching(evict = {@CacheEvict(value = ContentChannelCacheConstant.PAGE,allEntries = true),
        @CacheEvict(value = ContentChannelCacheConstant.LIST,allEntries = true)})
    @Override
    public Boolean deleteContentChannelVo(String[] checkedIds) {
        try {
            Boolean flag = contentChannelService.deleteContentChannel(checkedIds);
            return flag;
        } catch (Exception e) {
            log.error("删除交易渠道表异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(ContentChannelEnum.DEL_FAIL);
        }
    }

    @Override
    @Cacheable(value = ContentChannelCacheConstant.LIST,key ="#contentChannelVo.hashCode()")
    public List<ContentChannelVo> findContentChannelVoList(ContentChannelVo contentChannelVo) {
        try {
            List<ContentChannelVo> records = BeanConv.toBeanList(contentChannelService.findContentChannelList(contentChannelVo),ContentChannelVo.class);
            return records;
        } catch (Exception e) {
            log.error("删除交易渠道表异常：{}", ExceptionsUtil.getStackTraceAsString(e));
            throw new ProjectException(ContentChannelEnum.LIST_FAIL);
        }
    }
}
